﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Configure
{
    public partial class frmSuppliersFilter : Form
    {
        private string configurationFile = "configuration.xml";
        private ServiceConfig config { get; set; }

        public frmSuppliersFilter()
        {
            InitializeComponent();
            
            config = new ServiceConfig(this.configurationFile);
            this.FillListBoxs();
        }

        private void FillListBoxs()
        {
            try
            {
                var suppliers = config.GetSuppliers();
                var suppliersCache = config.GetSuppliersCache();

                this.RefreshSuppliersBox(suppliers);
                this.RefreshSuppliersCacheBox(suppliersCache);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            var data = fldSuppliers.Items.Cast<KV>().ToDictionary(r => r.Key, r => r.Value);
            config.SaveSuppliers(data);
            this.Close();
        }

        private void cmdRemove_Click(object sender, EventArgs e)
        {
            var item = fldSuppliers.SelectedItem;
            if (item != null)
                fldSuppliers.Items.Remove(item);
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            for (var i = 0; i < fldSuppliersCache.CheckedItems.Count; i++)
            {
                if (!fldSuppliers.Items.Contains(fldSuppliersCache.CheckedItems[i]))
                    fldSuppliers.Items.Add(fldSuppliersCache.CheckedItems[i]);
            }
        }

        private void fldFilter_TextChanged(object sender, EventArgs e)
        {
            var suppliersCache = config
                .GetSuppliersCache()
                .Where(r => r.Value.IndexOf(fldFilter.Text) > -1)
                .ToDictionary(r => r.Key, r => r.Value);

            this.RefreshSuppliersCacheBox(suppliersCache);
        }

        private void RefreshSuppliersCacheBox(Dictionary<string, string> data)
        {
            //fldSuppliersCache.DataSource = data
            //   .Select(r => new { r.Key, r.Value })
            //    .ToList();

            fldSuppliersCache.Items.Clear();
            fldSuppliersCache.Items.AddRange(data.Select(r => new KV { Key= r.Key, Value= r.Value }).ToArray());

            fldSuppliersCache.ValueMember = "Key";
            fldSuppliersCache.DisplayMember = "Value";
        }

        private void RefreshSuppliersBox(Dictionary<string, string> data)
        {
            //fldSuppliers.DataSource = data
            //    .Select(r => new { r.Key, r.Value })
            //    .ToList();

            fldSuppliers.Items.Clear();
            fldSuppliers.Items.AddRange(data.Select(r => new KV { Key= r.Key, Value= r.Value }).ToArray());

            fldSuppliers.ValueMember = "Key";
            fldSuppliers.DisplayMember = "Value";
        }

        class KV
        {
            public string Key { get; set; }
            public string Value { get; set; }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
